package com.xtaller.lazy.anync;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xtaller.lazy.lib.convert.S;
import com.xtaller.lazy.model.system.UserJwt;
import com.xtaller.lazy.service.system.UserJwtService;
import com.xtaller.lazy.util.CacheUtil;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-03
 * @Description: jwt 异步处理
 */
@Component
public class JwtTask {
    @Autowired private UserJwtService jwtService;
    @Autowired private CacheUtil cacheUtil;

    @Async("taskExecutor")
    public void updateJwt(Long userId, int platform,String token){
        var qw = new QueryWrapper<UserJwt>();
        qw.lambda()
                .eq(UserJwt::getUserId, userId)
                .eq(UserJwt::getPlatform, platform);
        var jwt = jwtService.getOne(qw);

        var key = S.apppend(platform, "-", userId);

        if(jwt == null){
            jwt = new UserJwt();
            jwt.setUserId(userId);
            jwt.setJwt(token);
            jwt.setPlatform(platform);
            jwtService.save(jwt);
        }else{
            jwt.setJwt(token);
            jwtService.updateById(jwt);
        }
        cacheUtil.set(key, jwt);
    }
}
